/**
 * 表结构初始化
 */

const { Sequelize, Model } = require('sequelize')
const { sequelize } = require('@core/db.js')
const { Category } = require('./category')

class Goods extends Model {
  static async getList() {
    const goodsList = await Goods.findAll({
      order: [
        ['id', 'DESC']
      ],
      include: [Category]
    })
    return goodsList
  }
}

Goods.init({
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  title: Sequelize.STRING,
  // categoryId: {
  //   type: Sequelize.INTEGER,
  //   // references: {
  //   //   model: Category,
  //   //   key: "categoryId"
  //   // }
  // }
  // category: Sequelize.Category
}, {
  sequelize,
  tableName: 'goods'
})

// Goods.hasOne(Category, { foreignKey: 'category_id' })
// Category.belongsTo(Goods)
// Category.belongsTo(Goods, { foreignKey: 'category_id', targetKey: 'id' })
// Goods.belongsTo(Category, { as: 'category', foreignKey: 'category_id' });

module.exports = {
  Goods
}